package org.lanqiao.api.cart;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.lanqiao.common.Constants;
import org.lanqiao.common.JsonResult;
import org.lanqiao.common.JsonResultWriter;
import org.lanqiao.pojo.CartListPOJO;
import org.lanqiao.service.CartServiceImpl;
import org.lanqiao.util.StringUtil;


/**
 * 加入购物车接口的实现
 * @author dzps
 *
 */
@WebServlet("/api/cart/add")

public class AddCartServlet extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String productCount = "1";
		JsonResult result=null;
		CartServiceImpl addCartService = new CartServiceImpl();
		//加入购物车传入参数,   	 	    
	    String userId = request.getParameter("UserId");
		String productId = request.getParameter("ProductId");
		if(StringUtil.empty(request.getParameter("ProductCount"))!=null) {
			productCount = StringUtil.empty(request.getParameter("ProductCount"));
		}		
		String totalPrice = StringUtil.empty(request.getParameter("TotalPrice"));
		
//		double a1 = Double.valueOf(totalPrice);
//		double b = Double.valueOf(productCount);
//		double sum = a1*b;
//		System.out.println(sum);
		CartListPOJO cartlist = new CartListPOJO();
		cartlist.setUserId(userId);
		cartlist.setProductId(productId);
		cartlist.setProductCount(productCount);
		cartlist.setTotalPrice(totalPrice);
		int a = addCartService.addCart(userId,productId,productCount,totalPrice);
		try {
			if(a>0){				
				result=new JsonResult<String>("加入成功",Constants.STATUS_SUCCESS);
			}else {				
				result=new JsonResult<String>("加入失败",Constants.STATUS_UNFOUND);
			}
		} catch (Exception e) {			
			result=new JsonResult<String>("加入异常",Constants.STATUS_FAILURE);
		}
		
		JsonResultWriter.writer(response, result);
		
	}


}